package org.kgc.alanhealth.controller;


import org.kgc.alanhealth.entity.HospitalUser;
import org.kgc.alanhealth.entity.HospitalUseraddress;
import org.kgc.alanhealth.entity.Sessiontoken;
import org.kgc.alanhealth.service.SessionTokenService;
import org.kgc.alanhealth.service.UserService;
import org.kgc.alanhealth.util.Result;
import org.kgc.alanhealth.util.SmsService;
import org.kgc.alanhealth.util.SmsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("user")
@RestController
@CrossOrigin(origins = "*")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private SessionTokenService service;
    @Autowired
    private SmsService smsService;
    @RequestMapping("login")
    public Result login(String username,String password){

        HospitalUser user=userService.getUserByUsername(username,password);
        if(user!=null){
            return Result.success("登录成功",user);
        }else {
            return Result.error("登录失败");
        }

    }

    @RequestMapping("address")
    public Result getaddress(int id){
        List<HospitalUseraddress> useraddresses =userService.getAddressByUserid(id);
        if(useraddresses!=null){
            return Result.success(useraddresses);
        }else {
            return Result.error("获取失败");
        }
    }
    @RequestMapping("quit")
    public Result quit(String token){

        if(service.deleteByPrimaryKey(token)){
            return Result.success("用户已退出");
        }else {
            return Result.error("退出失败");
        }

    }
    @RequestMapping("verify")
    public Result verifyToken(String userid) {
        if (userid!=null) {
            int id = Integer.parseInt(userid);
            HospitalUser user = userService.getUserById(id);
            return Result.success("自动登录成功", user);
        } else {
            return Result.error("验证失败");
        }
    }


    @PostMapping("register")
    public Result register(@RequestBody HospitalUser hospitalUser) {
        if(userService.getUserByUsername2(hospitalUser.getUsername())==null){
            HospitalUser user=new HospitalUser();
            user.setUsername(hospitalUser.getUsername());
            user.setUserpassword(hospitalUser.getUserpassword());
            if (user.getUserphone() == null) {
                user.setUserphone("默认手机号");
            }
            if (user.getHealthid() == null) {
                user.setHealthid(0);
            }
            if (user.getAddress() == null) {
                user.setAddress("默认地址");
            }
            if (userService.insertSelective(user)==1){
                return Result.success("注册成功");
            }else {
                return Result.error("注册失败");
            }
        }else{
            return Result.error("用户名已被注册");
        }
    }
    @GetMapping("code")
    public Result sendCode(@RequestParam String phone) {
        if(userService.getUserByUsername2(phone)==null){
            boolean sent = smsService.sendSms(phone, "微风医药","SMS_473310180");
            if (sent) {
                return Result.success("验证码发送成功");
            } else {
                return Result.error("验证码发送失败");
            }
        }else{
            return Result.error("该手机号已被注册");
        }

    }

    @GetMapping("register1")
    public Result verifyCode(@RequestParam String phone, @RequestParam String code) {
        boolean isCodeValid = smsService.verifyCode(phone, code);
        if (isCodeValid) {
            if(userService.getUserByUsername2(phone)==null){
                HospitalUser user=new HospitalUser();
                user.setUsername(phone);
                user.setUserpassword(phone);
                user.setUserphone(phone);
                user.setHealthid(0);
                user.setAddress("默认地址");
                if (userService.insertSelective(user)==1){
                    return Result.success("注册成功");
                }else {
                    return Result.error("注册失败");
                }
            }else{
                return Result.error("用户名已被注册");
            }
        } else {
            return Result.error("验证码不正确");
        }
    }


}